let departmentaddModule = (() => {
  let
    departmentId,
    departmentName = $('.inpBox').find('input'),
    departmentDesc = $('.inpBox').find('textarea'),
    submit = $('.submit');

  let fillDepartmentInfo = async function () {
    let res = await axios.get('/department/info', { params: { departmentId } });
    if (res.code) { alert('获取信息失败'); return };
    departmentName.val(res.data.name);
    departmentDesc.val(res.data.desc);
  }

  let handleSubmit = function () {
    let deliver = async function () {
      let baseParams = {
        name: departmentName.val().trim(),
        desc: departmentDesc.val().trim()
      }
      if (departmentId) {
        // 修改提交
        params = baseParams;
        params.departmentId = departmentId;
        let res = axios.post('/department/update', params)
        !res.code ? (alert('修改部门信息成功'), window.location.href = 'departmentlist.html') : alert('修改部门信息失败')
      } else {
        // 添加提交
        params = baseParams;
        let res = axios.post('/department/add', params);
        !res.code ? (alert('添加部门信息成功'), window.location.href = 'departmentlist.html') : alert('添加部门信息失败')
      }
    };
    submit.click(deliver)
  }
  return {
    init() {
      departmentId = window.location.href.getUrlEncodedParams().departmentId;
      // 编辑信息渲染
      departmentId ? fillDepartmentInfo() : null;
      // 提交按钮的事件绑定
      handleSubmit()
    }
  }
})();

departmentaddModule.init()